﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;

namespace BugTracker.BTGridView
{

    public delegate void BTGridViewPageIndexChangingHandler (object sender, BTPagerEventArgs e);

    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [ToolboxData("<{0}:BTGridView runat=server></{0}:BTGridView>")]
    public class BTGridView : GridView
    {
        
        #region fields

        private const string VIRTUAL_COUNT = "VIRTUAL_COUNT";
        private const string VIRTUAL_INDEX = "VIRTUAL_INDEX";
        private const string PAGER = "PAGER";
        private const string SORTER = "SORTER";

        #endregion

        #region properties

        public int VirtualCount
        {
            get { return (int?)ViewState[VIRTUAL_COUNT] ?? 0; }
            set { ViewState[VIRTUAL_COUNT] = value; }
        }

        public int VirtualIndex
        {
            get { return (int?)ViewState[VIRTUAL_INDEX] ?? 0; }
            set { ViewState[VIRTUAL_INDEX] = value; }
        }

        public Pager BTPager
        {
            get
            {
                return (Pager)ViewState[PAGER];
            }
            set
            {
                ViewState[PAGER] = value;
            }
        }

        public Sorter PTSorter
        {
            get
            {
                return (Sorter)ViewState[SORTER];
            }
            set
            {
                ViewState[SORTER] = value;
            }
        }

        #endregion

        #region events 

        public event BTGridViewPageIndexChangingHandler BTGridViewPageIndexChanging = null;

        public void OnBTGridViewPageIndexChanging(object sender, BTPagerEventArgs e)
        {
            if (BTGridViewPageIndexChanging != null)
            {
                BTGridViewPageIndexChanging(sender, e);
            }
        }

        #endregion

        #region initializePager

        protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
        {
            if (this.AllowPaging)
            {
                pagedDataSource.AllowCustomPaging = true;
                pagedDataSource.VirtualCount = VirtualCount;
                pagedDataSource.CurrentPageIndex = VirtualIndex;
            }
            base.InitializePager(row, columnSpan, pagedDataSource);
        }

        #endregion

        #region OnPageIndexChanging

        protected override void OnPageIndexChanging(GridViewPageEventArgs e)
        {
            this.VirtualIndex = e.NewPageIndex;
            this.PageIndex = e.NewPageIndex;
            this.OnBTGridViewPageIndexChanging(this, new BTPagerEventArgs(e.NewPageIndex));
        }

        #endregion

        #region OnPageIndexChanging

        protected override void OnSorting(GridViewSortEventArgs e)
        {
            if (PTSorter == null)
            {
                PTSorter = new Sorter(e.SortExpression, e.SortDirection);
            }
            Sorter sorter = PTSorter;
            sorter.Column = e.SortExpression;
            switch (sorter.SortDirection)
            {
                case SortDirection.Ascending:
                    sorter.SortDirection = SortDirection.Descending;
                    break;
                case SortDirection.Descending:
                    sorter.SortDirection = SortDirection.Ascending;
                    break;
            }
            PTSorter = sorter;
            this.OnBTGridViewPageIndexChanging(this, new BTPagerEventArgs(this.PageIndex));
        }

        #endregion

    }
}
